package JaggSoft;

import JaggSoft.Utilidades.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Solicitante extends javax.swing.JInternalFrame {

    private int codigo_solicitante;
    private boolean nuevo;
    private String id_bitacora;
    private JFileChooser selector;
    public Solicitante() {
        initComponents();
        this.setLocation(new Point(70, 0));
        selector=new JFileChooser();
        selector.setFileFilter(new FileNameExtensionFilter("Libro de Excel",new String[]{"xls","xlsx"}));
        selector.setMultiSelectionEnabled(false);
        selector.setSelectedFile(new File("solicitantes.xls"));
        Bloquear(true);
        BloquearTexto(true);
        CargarSolicitantes();
        codigo_solicitante = -1;
        if(Conexion.bitacora) id_bitacora=Util.IniciarBitacora(this.getTitle());
    }

    private void Bloquear(boolean estado) {
        jbtnNuevo.setEnabled(estado);
        jbtnGuardar.setEnabled(!estado);
        jbtnModificar.setEnabled(estado);
        jbtnEliminar.setEnabled(estado);
        jbtnCancelar.setEnabled(!estado);
        jtblSolicitante.setEnabled(estado);
    }

    private void BloquearTexto(boolean estado) {
        jtfNombre.setEditable(!estado);
        jtfTelefono.setEditable(!estado);
        jtfApellido.setEditable(!estado);
        jtfIdentidad.setEditable(!estado);

    }

    private void Limpiar() {
        jtfNombre.setText("");
        jtfTelefono.setText("");
        jtfApellido.setText("");
        jtfIdentidad.setText("");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfNombre = new javax.swing.JTextField();
        jtfApellido = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtfIdentidad = new javax.swing.JTextField();
        jtfTelefono = new javax.swing.JTextField();
        jchFormatearTelefono = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jbtnCancelar = new javax.swing.JButton();
        jbtnEliminar = new javax.swing.JButton();
        jbtnModificar = new javax.swing.JButton();
        jbtnGuardar = new javax.swing.JButton();
        jbtnNuevo = new javax.swing.JButton();
        jbtnExportar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtblSolicitante = new javax.swing.JTable();

        setClosable(true);
        setTitle("Solicitantes");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/solicitantes.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentMoved(java.awt.event.ComponentEvent evt) {
                formComponentMoved(evt);
            }
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Solicitante"));

        jLabel1.setText("Nombres:");

        jLabel2.setText("Apellidos:");

        jLabel3.setText("Identidad:");

        jLabel4.setText("Teléfono:");

        jtfIdentidad.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtfIdentidadKeyTyped(evt);
            }
        });

        jtfTelefono.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtfTelefonoKeyTyped(evt);
            }
        });

        jchFormatearTelefono.setSelected(true);
        jchFormatearTelefono.setText("Auto formatear");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfApellido, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jchFormatearTelefono))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(6, 6, 6)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jtfIdentidad, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                            .addComponent(jtfNombre))))
                .addContainerGap(10, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfIdentidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jchFormatearTelefono))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Acciones"));

        jbtnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_cancelar.png"))); // NOI18N
        jbtnCancelar.setToolTipText("Cancelar");
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(evt);
            }
        });

        jbtnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_eliminar.png"))); // NOI18N
        jbtnEliminar.setToolTipText("Eliminar");
        jbtnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEliminarActionPerformed(evt);
            }
        });

        jbtnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_modificar.png"))); // NOI18N
        jbtnModificar.setToolTipText("Modificar");
        jbtnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnModificarActionPerformed(evt);
            }
        });

        jbtnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_guardar.png"))); // NOI18N
        jbtnGuardar.setToolTipText("Guardar");
        jbtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGuardarActionPerformed(evt);
            }
        });

        jbtnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/accion_nuevo.png"))); // NOI18N
        jbtnNuevo.setToolTipText("Nuevo");
        jbtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNuevoActionPerformed(evt);
            }
        });

        jbtnExportar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recursos/excel.png"))); // NOI18N
        jbtnExportar.setToolTipText("Exportar a Excel");
        jbtnExportar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnExportarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnExportar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jbtnExportar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jtblSolicitante.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblSolicitanteMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jtblSolicitante);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 458, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(76, 76, 76)
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(114, 114, 114)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CargarSolicitantes() {
        Util.CargarTabla(jtblSolicitante,"SELECT id_solicitante,identidad,nombres,apellidos,telefono FROM solicitante",new String[]{"Id","Identidad","Nombre","Apellido","Telefono"});
        Util.OcultarColumna(jtblSolicitante,0);
    }

    private void SeleccionarDatos() {
        int fila = jtblSolicitante.getSelectedRow();
        if (fila != -1) {
            codigo_solicitante = (int) jtblSolicitante.getValueAt(fila, 0);
            jtfIdentidad.setText((String) jtblSolicitante.getValueAt(fila, 1));
            jtfNombre.setText((String) jtblSolicitante.getValueAt(fila, 2));
            jtfApellido.setText((String) jtblSolicitante.getValueAt(fila, 3));
            jtfTelefono.setText((String) jtblSolicitante.getValueAt(fila, 4));
        }
    }
    private void jbtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelarActionPerformed
        Limpiar();
        Bloquear(true);
        BloquearTexto(true);
        SeleccionarDatos();
    }//GEN-LAST:event_jbtnCancelarActionPerformed
    private void jbtnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEliminarActionPerformed
        if (jtblSolicitante.getSelectedRow() != -1) {
            if (JOptionPane.showConfirmDialog(null, "¿Esta seguro de eliminar el solicitante y todo lo relacionado con éste?", "Administración de Solicitantes", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                if (Conexion.EjecutarConsulta("DELETE FROM solicitante Where id_solicitante=" + jtblSolicitante.getValueAt(jtblSolicitante.getSelectedRow(), 0))) {
                    if(Conexion.bitacora) Util.AccionBitacora(id_bitacora,"Eliminar Solicitante: "+Util.ComponerTexto(jtfNombre.getText()+" "+jtfApellido.getText()));
                    CargarSolicitantes();
                    Limpiar();
                    JOptionPane.showMessageDialog(null, "¡Solicitante Eliminado!", "Administración de Solicitantes", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        } else {
            JOptionPane.showMessageDialog(null, "Debe seleccionar un solicitante", "Administración de Solicitantes", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jbtnEliminarActionPerformed

    private void jbtnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnModificarActionPerformed
        Bloquear(false);
        BloquearTexto(false);
        nuevo = false;
    }//GEN-LAST:event_jbtnModificarActionPerformed
    private boolean ValidarCampos(){
        String msj=null;
        msj=(!jtfIdentidad.getText().equals("")?(ContarNumeros(jtfIdentidad.getText())!=13?"El número de identidad debe contener 13 digitos.":""):"Falta ingresar el número de identidad");
        msj+=(jtfNombre.getText().equals("")?(msj.equals("")?"Falta ingresar el nombre":", el nombre"):"");
        msj+=(jtfApellido.getText().equals("")?(msj.equals("")?"Falta ingresar el apellido":", el apellido"):"");
        msj+=(jtfTelefono.getText().equals("")?(msj.equals("")?"Falta ingresar el telefono":", el apellido"):"");
        if(!msj.equals("")) JOptionPane.showMessageDialog(null,msj+".","Falta ingresar algunos campos",JOptionPane.WARNING_MESSAGE);
        return msj.equals("");
    }
    private void jbtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGuardarActionPerformed
        if (ValidarCampos()) {
            if (nuevo) {
                if (Conexion.EjecutarConsulta("INSERT INTO solicitante(nombres,apellidos,telefono,identidad) VALUES('"
                        +Util.ComponerTexto(jtfNombre.getText())+ "','"+Util.ComponerTexto(jtfApellido.getText())+"','"
                        +Util.ComponerTexto(jtfTelefono.getText())+"','"+GetIdentidadFormateada(jtfIdentidad.getText())+"');")) {
                            if(Conexion.bitacora) Util.AccionBitacora(id_bitacora,"Guardar Solicitante: "+Util.ComponerTexto(jtfNombre.getText()+" "+jtfApellido.getText()));
                            CargarSolicitantes();
                            JOptionPane.showMessageDialog(null, "¡Solicitante Agregado!", "Administración de Solicitantes", JOptionPane.INFORMATION_MESSAGE);
                            Bloquear(true);
                            BloquearTexto(true);
                            Limpiar();
                } else {
                    JOptionPane.showMessageDialog(null, "No se pudo agregar el solicitante", "Administración de Solicitantes", JOptionPane.ERROR_MESSAGE);
                }
            }  
            else {
                if (codigo_solicitante>0) {
                    if (Conexion.EjecutarConsulta("UPDATE solicitante SET nombres='"+Util.ComponerTexto(jtfNombre.getText())+"',apellidos='"
                            +Util.ComponerTexto(jtfApellido.getText())+"',telefono='"+Util.ComponerTexto(jtfTelefono.getText())+"',identidad='"
                            +GetIdentidadFormateada(jtfIdentidad.getText())+"' WHERE id_solicitante="+codigo_solicitante)) {
                                if(Conexion.bitacora) Util.AccionBitacora(id_bitacora,"Modificar Solicitante: "+Util.ComponerTexto(jtfNombre.getText()+" "+jtfApellido.getText()));
                                CargarSolicitantes();
                                JOptionPane.showMessageDialog(null, "¡Solicitante Modificado!", "Administración de Solicitantes", JOptionPane.INFORMATION_MESSAGE);
                                Bloquear(true);
                                BloquearTexto(true);
                                Limpiar();
                    } else {
                        JOptionPane.showMessageDialog(null, "No se pudo modificar el solicitante", "Administración de Solicitantes", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Debe seleccionar un solicitante", "Administración de Solicitantes", JOptionPane.WARNING_MESSAGE);
                }
            }
        }
    }//GEN-LAST:event_jbtnGuardarActionPerformed
    private String GetIdentidadFormateada(String identidad){
        identidad=identidad.replace("-","");
        return identidad.substring(0,4)+"-"+identidad.substring(4,8)+"-"+identidad.substring(8);
    }
    private String GetNumeroTelefonoFormateado(String telefono){
        telefono=telefono.replaceAll("-","");
        return telefono.substring(0,4)+"-"+telefono.substring(4);
    }
    private void CargarDatos(int fila) {
        jtfIdentidad.setText(jtblSolicitante.getValueAt(fila, 1).toString());
        jtfNombre.setText(jtblSolicitante.getValueAt(fila, 2).toString());
        jtfApellido.setText(jtblSolicitante.getValueAt(fila, 3).toString());
        jtfTelefono.setText(jtblSolicitante.getValueAt(fila, 4).toString());
        codigo_solicitante = new Integer(jtblSolicitante.getValueAt(fila, 0).toString());
    }
    private void jbtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNuevoActionPerformed
        Bloquear(false);
        BloquearTexto(false);
        Limpiar();
        nuevo = true;
        codigo_solicitante=0;
    }//GEN-LAST:event_jbtnNuevoActionPerformed
    private int ContarNumeros(String texto){
        int n=0;
        for(char c:texto.toCharArray())
            if("0123456789".contains(""+c))
                n++;
        return n;
    }
    private void formComponentMoved(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentMoved
        if(evt.getComponent().getLocation().x<70)
            evt.getComponent().setLocation(70,evt.getComponent().getLocation().y);
        if(evt.getComponent().getLocation().y<0)
            evt.getComponent().setLocation(evt.getComponent().getLocation().x,0);
    }//GEN-LAST:event_formComponentMoved
    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        if(this.getParent().getWidth()-70<this.getWidth()  && this.isMaximum)
            this.setSize(this.getWidth()-70,this.getHeight());
    }//GEN-LAST:event_formComponentResized
    private void jtblSolicitanteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblSolicitanteMouseClicked
        if(jtblSolicitante.getSelectedRow()>-1){
            CargarDatos(jtblSolicitante.getSelectedRow());
        }
    }//GEN-LAST:event_jtblSolicitanteMouseClicked
    private void jtfIdentidadKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfIdentidadKeyTyped
        String dato=(jtfIdentidad.getText()+evt.getKeyChar()).replaceAll("-","");
        if(!Util.esNumero(dato,false))
            evt.consume();
        if(ContarNumeros(dato)>13)
            evt.consume();
    }//GEN-LAST:event_jtfIdentidadKeyTyped
    private void jtfTelefonoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfTelefonoKeyTyped
        if((int)evt.getKeyChar()!=KeyEvent.VK_BACK_SPACE){
            String dato=(jtfTelefono.getText()+evt.getKeyChar()).replaceAll("-","");
            if (!Util.esNumero(dato, false))
                evt.consume();
            if(jchFormatearTelefono.isSelected()){
                if(ContarNumeros(jtfTelefono.getText())==4 && jtfTelefono.getText().length()==4)
                    jtfTelefono.setText(jtfTelefono.getText()+"-");
                if(ContarNumeros(jtfTelefono.getText())>=8)
                    evt.consume();
            }
        }
    }//GEN-LAST:event_jtfTelefonoKeyTyped
    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        if(Conexion.bitacora) Util.ClausurarBitacora(id_bitacora);
    }//GEN-LAST:event_formInternalFrameClosing

    private void jbtnExportarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnExportarActionPerformed
        if(jtblSolicitante.getRowCount()>0){
            if(selector.showSaveDialog(null)==JOptionPane.OK_OPTION){
                try {
                    String ruta=selector.getSelectedFile().getAbsolutePath();
                    if(!ruta.endsWith(".xls"))
                        ruta+=".xls";
                    File archivo_excel=new File(ruta);
                    switch(Util.ExportarExcel(jtblSolicitante,archivo_excel,"Solicitantes")){
                        case OK:
                        JOptionPane.showMessageDialog(null,"Archivo Exportado",this.getTitle(),JOptionPane.INFORMATION_MESSAGE);
                        Desktop.getDesktop().open(archivo_excel);
                        break;
                        case CANCELADO:
                        break;
                        case ERROR:
                        JOptionPane.showMessageDialog(null,"No se pudo exportar el archivo","Error",JOptionPane.ERROR_MESSAGE);
                        break;
                    }
                } catch (Exception ex) {System.err.println(ex.getMessage());}
            }
        }
        else
            JOptionPane.showMessageDialog(null,"No hay datos para exportar","Sin Datos",JOptionPane.WARNING_MESSAGE);
    }//GEN-LAST:event_jbtnExportarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JButton jbtnEliminar;
    private javax.swing.JButton jbtnExportar;
    private javax.swing.JButton jbtnGuardar;
    private javax.swing.JButton jbtnModificar;
    private javax.swing.JButton jbtnNuevo;
    private javax.swing.JCheckBox jchFormatearTelefono;
    private javax.swing.JTable jtblSolicitante;
    private javax.swing.JTextField jtfApellido;
    private javax.swing.JTextField jtfIdentidad;
    private javax.swing.JTextField jtfNombre;
    private javax.swing.JTextField jtfTelefono;
    // End of variables declaration//GEN-END:variables
}